<extend name="Public/baseConf"/>
<block name="breadcrumb"> 
	<div class="row wrapper border-bottom white-bg page-heading">
	    <div class="col-lg-6">
	        <h2><present name="data">编辑<else />新增</present>钩子</h2>
	        <ol class="breadcrumb">
	            <li><a href="{:U('index/index')}">首页</a></li>
	            <li><a href="{:U('Addons/hooks')}">钩子列表</a></li>
	            <li class="active">
	                <strong><present name="data">编辑<else />新增</present>钩子</strong>
	            </li>
	        </ol>
	    </div>
	    <div class="col-lg-6">
            <div class="title-action">
	            	<button id="subbutton" class="btn btn-primary" type="button" ><i class="fa fa-check"></i>保存</button>
		             <a class="sort_cancel btn btn-warning" type="submit" url="{$Think.cookie.__forward__}">
	            	<i class="fa fa-reply"></i>返回</a>
 			</div>
        </div>
	</div>
</block>

<block name="body">
	<div class="row">
	    <div class="col-lg-12">
	        <div class="ibox float-e-margins">
	            <div class="ibox-title">
	                <h5>请在下表中填入信息<small></small></h5>
	                <div class="ibox-tools">
	                    <a class="collapse-link">
	                        <i class="fa fa-chevron-up"></i>
	                    </a>
	                </div>
	            </div>
	            <div class="ibox-content">
	                <form id='form' action="{:U('updateHook')}" method="post" class="form-horizontal">
	                    <div class="form-group"><label class="col-sm-3 control-label">钩子名称<span class="check-tips">（需要在程序中先添加钩子，否则无效）</span></label>
	                        <div class="col-sm-9"><input type="text" value="{$data.name}" name="name"  class="form-control"></div>
	                    </div>
						<div class="hr-line-dashed"></div>
	                    <div class="form-group"><label class="col-sm-3 control-label">钩子描述<span class="check-tips">（钩子的描述信息）</span></label>
	                        <div class="col-sm-9"><textarea name="description" class="form-control">{$data.description}</textarea> </div>
	                    </div>
						<div class="hr-line-dashed"></div>
	                    <div class="form-group"><label class="col-sm-3 control-label">钩子类型<span class="check-tips">（区分钩子的主要用途）</span></label>
	                        <div class="col-sm-9">
								<select name="type" class="form-control">
									<volist name=":C('HOOKS_TYPE')" id="vo">
										<option value="{$key}" <eq name="data.type" value="$key"> selected</eq>>{$vo}</option>
									</volist>
								</select>
							</div>
	                    </div>
						<div class="hr-line-dashed"></div>
						<present name="data">
		                    <div class="form-group">
		                    	<label class="col-sm-3 control-label">钩子挂载的插件排序<span class="check-tips">（拖动后保存顺序，影响同一个钩子挂载的插件执行先后顺序）</span></label>
		                        <div class="col-sm-9">
									<input hidden name="addons" value="{$data.addons}" readonly>
									<empty name="data.addons">
										暂无插件，无法排序
									<else />
										<div class="dd" id="sortUl">
			                                <ol class="dd-list">
				                                <volist name=":explode(',',$data['addons'])" id="addons_vo">
													<li class="dd-item" data-id="1">
				                                        <div class="dd-handle"><b><i class="fa fa-times"></i>&nbsp;</b><em>{$addons_vo}</em></div>
				                                    </li>
												</volist>
			                                </ol>
			                            </div>
										<script type="text/javascript">
											$(function(){
									            $('#sortUl').nestable({
									                group: 1
									            }).on('change', updateVal);
									            
												$('#sortUl ol li b').click(function(){
					                            	$(this).parent().remove();
					                            	updateVal();
					                            });
												// 更新排序后的隐藏域的值
						                        function updateVal() {
						                        	var sortVal = [];
					                            	$('#sortUl ol li').each(function(){
					                            		sortVal.push($('em',this).text());
					                            	});
					                                $("input[name='addons']").val(sortVal.join(','));
						                        }
											})
										</script>
									</empty>
								</div>
		                    </div>
							<div class="hr-line-dashed"></div>
						</present>
						<input type="hidden" name="id" value="{$data.id}">
	            			
	                </form>
	            </div>
	        </div>
	    </div>
	</div> 
</block>

<block name="script">
	<present name="data">
		<!-- Nestable List -->
    	<script src="__INSPINIA__/js/plugins/nestable/jquery.nestable.js"></script>
	</present>
	<script type="text/javascript">
	//获取排序并提交
	$('#subbutton').click(function(){ 
    		var self = $('#form');
    		$.post(self.attr("action"), self.serialize(), success, "json");
    		return false;
    		function success(data){
				if (data.status) {
                    updateAlert(data.info + ' 页面即将自动跳转~','alert-success');
                }else{
                    updateAlert(data.info,'alert-success');
                }
                setTimeout(function(){
                    if (data.status) {
                    	$('.sort_cancel').click();
                    }
                },1500);
    		}
	});
	//点击取消按钮
	$('.sort_cancel').click(function(){
		window.location.href = $(this).attr('url');
	});
	</script>
</block>